<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="open(0,1)">添加权限</el-button>
      <!--
        el-table支持树形数据展示(但是有条件):
          1. 必须含有一个字段children(可以改配置) :tree-props="{ children: 'children' }"
          2. 必须指定一个row-key="id"    指定一个唯一标识的字段名字符串  id
       -->
      <el-table :data="list" row-key="id" :tree-props="{ children: 'child' }" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button v-if="row.type===1" size="mini" type="text" @click="open(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="edit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { findChildren } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],

      showDialog: false,

      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 决定了你是一级 还是二级    1    2
        pid: '', // 决定了你的父是谁  如果你是一级页面访问权 pid=0   二级按钮操作权 pid=?
        enVisible: '0' // 没有具体的功能  给'0' '1' 效果是一模一样的
      },

      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(res)

      // this.list = findChildren(res, 最大的父级的id) // 列表数据
      this.list = findChildren(res, 0) // 树形数据
    },
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 决定了你是一级 还是二级    1    2
        pid: '', // 决定了你的父是谁  如果你是一级页面访问权 pid=0   二级按钮操作权 pid=?
        enVisible: '0' // 没有具体的功能  给'0' '1' 效果是一模一样的
      }
      this.$refs.perForm.resetFields() // 只能清部分数据
      this.showDialog = false
    },
    btnOK() {
      // 点了确定
      this.$refs.perForm.validate().then(async() => {
        if (this.formData.id) {
          await updatePermission(this.formData)
          this.$message.success('编辑成功')
        } else {
          await addPermission(this.formData)
          this.$message.success('添加成功')
        }

        this.getPermissionList()
        this.btnCancel()
      }).catch(() => {})
    },
    open(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    del(id) {
      this.$confirm('你确定么?').then(async() => {
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      })
    },
    async edit(id) {
      const res = await getPermissionDetail(id)
      // console.log(res)
      this.formData = res

      this.showDialog = true
    }
  }
}
</script>

<style>
.btn-add {
  margin: 10px;
}
</style>
